<?php
/*
* @package Featured Posts
* @author Nando Pappalardo e Giustino Borzacchiello
* @version 1.4
*/
/*
Plugin Name: Featured Post with thumbnail
Plugin URI: http://www.yourinspirationweb.com/en/wordpress-plugin-featured-posts-with-thumbnails-highlighting-your-best-articles/
Description: This widget allows you to add in your blog's sidebar a list of featured post with thumbanil.
Author: Nando Pappalardo e Giustino Borzacchiello
Version: 1.4
Author URI: http://en.yourinspirationweb.com/

USAGE:

LICENCE:

This program is free software: you catccan redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/**
* Determina il percorso del plugin
* Determine plugin path
*/

$featured_post_plugin_path = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__)).'/';

if(function_exists('add_theme_support')) {
add_theme_support( 'post-thumbnails' );
}


/**
* Aggiunge il CSS del plugin
* Enqueue plugin CSS file
*/

/*
function featured_post_css() {
global $featured_post_plugin_path;
wp_enqueue_style('featured-post-css',$featured_post_plugin_path.'featured-post.css');
}
add_action('wp_print_styles', 'featured_post_css');
*/

/**
* Recupera la prima immagine del post
* Returns the first image in the post
*
*/

function catch_that_image() {
global $post, $posts, $featured_post_plugin_path;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

if(empty($first_img)) { //Defines a default image
 $first_img = $featured_post_plugin_path . "images/default.gif";
}
 return $first_img;
}


/**
*
* Mostra i post in evidenza
* Show featured posts using unordered list
* @param mixed $args
*/
function featured_posts_YIW( $args = null ) {

global $featured_post_plugin_path;
$defaults = array(
'title'	      => 'Лучшие статьи',
'numberposts'  => 5,
'orderby'      => 'DESC',
'widththumb'   => 73,
'heightthumb'  => 73,
'beforetitle'  => '',
'aftertitle'   => ''
);

$fp = wp_parse_args($args, $defaults);
/* User-selected settings. */

$showposts    = $fp['numberposts'];
$orderby      = $fp['orderby'];
$width_thumb  = $fp['widththumb'];
$height_thumb = $fp['heightthumb'];
$before_title = $fp['beforetitle'];
$after_title	 = $fp['aftertitle'];




/* Titolo del widget (before e after definiti dal tema). */
if ( ! empty($title) ) {
echo $before_title . $title . $after_title;
}

/*
* Modificare i parametri di questa funzione per mostrare/escludere categorie, pagine.
* If you want to exclude categories and/or pages modify this function's arguments properly
* Info: http://codex.wordpress.org/Template_Tags/get_posts
*/
global $post;
$featured_posts = get_posts('meta_key=featured&meta_value=1&numberposts='.$showposts.'&orderby='.$orderby);


/*
object(stdClass)#4085 (24) { ["ID"]=> int(2439) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-09-06 09:28:43" ["post_date_gmt"]=> string(19) "2012-09-06 09:28:43" ["post_content"]=> string(579)
*/

foreach ($featured_posts as $post):

?>

<table class="wftab nov" cellspacing="0" rules="none">
<col style="width:20px;"><col style="width:220px;"><col style="width:10px;">
<tbody>
<tr>
<td></td>
<td style="background-color:#fff;">
<div>
<img class="noim" src="<?php echo $featured_post_plugin_path ?>scripts/timthumb.php?src=<?php echo catch_that_image() ?>&amp;h=<?php echo $height_thumb ?>&amp;w=<?php echo $width_thumb ?>&amp;zc=1" class="imgleft" alt="<?php the_title(); ?>" alt="" width="73" height="73" />
<a class="titx titfon" href="<?php the_permalink(); ?>"><em><?php the_title(); ?></em></a>
<p class="datx"><? echo $post->post_date_gmt;?></p>
</div>
</td>
<td></td>
</tr><tr>
<td></td>
<td style="background-color:#fff;">
<div>
<?php
$tx = $post -> post_content;
$tx = strip_tags($tx);
$tx = strip_tags(substr($tx,0,801));
echo "<p class=\"ntx\">".substr($tx,0,800)."</p>";


?>
</div>
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
</td>
<td></td>
</tr>

</tbody>
</table>
  <?php
endforeach;

}

/**
* WIDGET SECTION
*-----------------------------------------------------------------------*/

/*
* Aggiunge file per la traduzione
* add translate language
*/
load_plugin_textdomain('featured-post','wp-content/plugins/featured-posts/language/');

/*
* Aggiungiamo la nostra funzione al gancio widgets_init
* Add our function to widgets_init hook
*/
add_action( 'widgets_init', 'my_widget_featured_posts' );

/*
* Funzione che registra il nostro widget
* Register our widget
*/
function my_widget_featured_posts() {
register_widget( 'Featured_posts' );
}

class Featured_posts extends WP_Widget {

function Featured_posts() {
/* Impostazione del widget */
$widget_ops = array( 'classname' => 'widget_featured-posts', 'description' => __('С помощью этого виджета Вы сможете показать в сайдбаре блога лучшие статьи и миниатюры к ним.','featured-post') );

/* Impostazioni di controllo del widget */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'widget_featured-posts' );

/* Creiamo il widget */
$this->WP_Widget( 'widget_featured-posts', __('Лучшие статьи','featured-post'), $widget_ops, $control_ops );
}

function widget( $args, $instance ) {
extract( $args );
$arguments = array(
'title'	     => $instance['title'],
'numberposts'  => $instance['showposts'],
'orderby'	     => $instance['orderby'],
'widththumb'   => $instance['width-thumb'],
'heightthumb'  => $instance['height-thumb'],
'beforetitle'  => $before_title,
'aftertitle'   => $after_title
);
global $featured_post_plugin_path;
/* Before widget (definito dal tema). */
echo $before_widget;
featured_posts_YIW($arguments);
echo $after_widget;
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags (if needed) and update the widget settings. */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['showposts'] = $new_instance['showposts'];
$instance['orderby'] = $new_instance['orderby'];
$instance['width-thumb'] = $new_instance['width-thumb'];
$instance['height-thumb'] = $new_instance['height-thumb'];

return $instance;
}

function form( $instance ) {
/* Impostazioni di default del nostro widget */
$defaults = array( 'title' => __('Лучшие статьи','featured-post'), 'showposts' => '', 'orderby' => '', 'width-thumb' => '73', 'height-thumb' => '73' );

$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Заголовок:','featured-post') ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>

<!-- numero di post da visualizzare -->
<p>
<select name="<?php echo $this->get_field_name( 'showposts' ); ?>" id="<?php echo $this->get_field_id( 'showposts' ); ?>" >
<?php
for ($i=0; $i<=100; $i++) {
if ($instance['showposts'] == $i) {
$selected = " selected='selected'";
} else {
$selected = "";
}
echo '<option class="level-0" value="'.$i.'"'.$selected.'>'.$i.'</option>';
}
?>
</select>
<label for="<?php echo $this->get_field_id( 'showposts' ); ?>"><?php _e('Сколько показывать статей?','featured-post') ?></label>
</p>

<!-- scelta del tipo di ordinamento -->
<p>
<select name="<?php echo $this->get_field_name( 'orderby' ); ?>" id="<?php echo $this->get_field_id( 'orderby' ); ?>" >
<option class="level-0" value="rand" <?php if ($instance['orderby'] == "random") echo " selected='selected'" ?>><?php _e('Произвольно','featured-post') ?></option>
<option class="level-0" value="title" <?php if ($instance['orderby'] == "title") echo " selected='selected'" ?>><?php _e('По заголовку','featured-post') ?></option>
<option class="level-0" value="date" <?php if ($instance['orderby'] == "date") echo " selected='selected'" ?>><?php _e('По дате','featured-post') ?></option>
<option class="level-0" value="author" <?php if ($instance['orderby'] == "author") echo " selected='selected'" ?>><?php _e('От автора','featured-post') ?></option>
<option class="level-0" value="modified" <?php if ($instance['orderby'] == "modified") echo " selected='selected'" ?>><?php _e('Изменённые','featured-post') ?></option>
<option class="level-0" value="ID" <?php if ($instance['orderby'] == "ID") echo " selected='selected'" ?>><?php _e('ID','featured-post') ?></option>
</select>
<label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e('Как выводить статьи:','featured-post') ?></label>
</p>

<p>
<input id="<?php echo $this->get_field_id( 'width-thumb' ); ?>" name="<?php echo $this->get_field_name( 'width-thumb' ); ?>" value="<?php echo $instance['width-thumb']; ?>" style="width:20%;" />
<label for="<?php echo $this->get_field_id( 'width-thumb' ); ?>"><?php _e('Ширина миниатюры','featured-post') ?></label>
</p>

<p>
<input id="<?php echo $this->get_field_id( 'height-thumb' ); ?>" name="<?php echo $this->get_field_name( 'height-thumb' ); ?>" value="<?php echo $instance['height-thumb']; ?>" style="width:20%;" />
<label for="<?php echo $this->get_field_id( 'height-thumb' ); ?>"><?php _e('Высота миниатюры','featured-post') ?></label>
</p>
<?php

}
}//end class Featured_posts

/**
* Aggiunge/rimuove il campo personalizzato featured
* Add/remove featured custom field
*
* @param integer $post_ID
*/
function add_featured($post_ID) {
$articolo = get_post($post_ID);

if ($_POST['insert_featured_post'] == 'yes') {
add_post_meta($articolo->ID, 'featured', 1, TRUE) or update_post_meta($articolo->ID, 'featured', 1);
}
elseif ( $_POST['insert_featured_post'] == 'no' ) {
delete_post_meta($articolo->ID, 'featured');
}
}

/**
*
* Mostra il form featured nella sezione "Scrivi Post"
* Shows featured form in "Write Post" section
*/

function post_box() {
global $post;
$featured = get_post_meta($post->ID,featured,1);
?>
<label for="insert_featured_post"><?php _e('Это будет лучшая статья?','featured-post') ?></label>
<select name="insert_featured_post" id="insert_featured_post">
<option value="yes" <?php if ($featured) echo 'selected="selected"'?>><?php _e('Да','featured-post') ?>&nbsp;</option>
<option value="no" <?php if (!$featured) echo 'selected="selected"'?>><?php _e('Нет ','featured-post') ?>&nbsp;</option>
</select>
<?php
}
function my_post_options_box() {
add_meta_box('post_info', __('Лучшая','featured-post'), 'post_box', 'post', 'side', 'high');
}


add_action('admin_menu', 'my_post_options_box');

add_action('new_to_publish', 'add_featured');
add_action('save_post', 'add_featured');

?>
